//
//  AuthenticateTableViewController.m
//  LPTClient
//
//  Created by apple on 2018/12/7.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "AuthenticateTableViewController.h"
#import "AuthenCommTableViewCell.h"
#import "AuthenTextViewTableViewCell.h"
#import "AuthenImgsTableViewCell.h"

@interface AuthenticateTableViewController ()

@property (nonatomic, strong) NSArray *m_array;

@property (nonatomic, strong) NSArray *image_array;

@end

@implementation AuthenticateTableViewController

+ (instancetype)instance
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    AuthenticateTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"AuthenticateTableViewControllerID"];
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UserInfo *userInfo = [LTPCacheManager shareInstance].getUserInfoModel.doctor;

    NSString *url = userInfo.credential.figure_file.url;
    NSString *name = userInfo.credential.name;
    NSString *sex = @"";
    if (userInfo.credential.sex == 1) {
        sex = @"男";
    }else if (userInfo.credential.sex == 2) {
        sex = @"女";
    }
    NSString *identity = userInfo.credential.identity;
    
    url = url?url:@"";
    
    NSString *status = @"";
    if (userInfo.credential.status == 1) {
        status = @"审核通过";
    }else if (userInfo.credential.status == 2) {
        status = @"审核失败";
    }
    NSString *hospotal = userInfo.credential.hospital;
    
    NSString *introduce = userInfo.credential.introduce;
    NSString *major = userInfo.credential.major;
    
    ImageInfo *imgInfo1 = userInfo.credential.identity_front_file;
    ImageInfo *imgInfo2 = userInfo.credential.identity_back_file;
    NSArray *certificates = userInfo.credential.certificates;

    NSMutableArray *mutableArr = [NSMutableArray array];
    if (imgInfo1) {
        [mutableArr addObject:imgInfo1];
    }
    if (imgInfo2) {
        [mutableArr addObject:imgInfo2];
    }
    if ([certificates count] > 0) {
        [mutableArr addObjectsFromArray:certificates];
    }
    self.image_array = [mutableArr copy];
    self.m_array = @[
                     @[
                         @{ @"title" : @"头像" , @"value":url, @"hasDetail" : @(0)},
                         @{@"title" : @"姓名", @"value":name, @"hasDetail" : @(0)},
                         @{@"title" : @"性别", @"value":sex, @"hasDetail" : @(0)},
                         @{@"title" : @"身份证号码", @"value":identity , @"hasDetail" : @(0)}
                         ],
                     @[
                         @{ @"title" : @"状态" , @"value":status, @"hasDetail" : @(0)}
                         ],
                     @[
                         @{@"title" : @"医院", @"value":hospotal, @"hasDetail" : @(1)},
                         @{@"title" : @"医生简介", @"value":introduce, @"hasDetail" : @(1)},
                         @{@"title" : @"专业擅长", @"value":major, @"hasDetail" : @(1)},
                         @{@"title" : @"证件图片", @"value":@"", @"hasDetail" : @(1)}
                         ]
                     ];
    
    
    UIView *v = [[UIView alloc] init];
    self.tableView.tableFooterView = v;
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.m_array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ((NSArray *)self.m_array[section]).count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    NSDictionary *dict = self.m_array[section][row];
    
    if (section == 0) {
        AuthenCommTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuthenCommTableViewCell" forIndexPath:indexPath];
        cell.titleLab.text = dict[@"title"];
        NSString *value = dict[@"value"];
        if (row == 0) {
            [cell.iconImageView setImageURL:[NSURL URLWithString:IMG_URL(value)]];
        }else {
            cell.descLab.text = value;
        }
        return cell;
    }else if (section == 1) {
        AuthenCommTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuthenCommTableViewCell" forIndexPath:indexPath];
        cell.titleLab.text = self.m_array[section][row][@"title"];
        NSString *value = dict[@"value"];
        cell.descLab.text = value;
        return cell;
    }else if (section == 2) {
        NSString *value = dict[@"value"];
        
        if (row == 0) {
            AuthenCommTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuthenCommTableViewCell" forIndexPath:indexPath];
            cell.titleLab.text = self.m_array[section][row][@"title"];
            cell.descLab.text = value;
            return cell;
        }else if (row == [self.m_array[section] count] - 1) {
            AuthenImgsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuthenImgsTableViewCell" forIndexPath:indexPath];
            cell.titleLab.text = self.m_array[section][row][@"title"];
            [cell refreshImages:self.image_array];
            return cell;
        }else {
            
            AuthenTextViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AuthenTextViewTableViewCell" forIndexPath:indexPath];
            cell.titleLab.text = self.m_array[section][row][@"title"];
            cell.descLab.text = value;
            return cell;
        }
    }
    
    return nil;
}


@end
